{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=1007
  Verdict: Accepted
}
var i,n,k,m,c:longint; t:string;
function inttostr(n:int64):string;
begin
str(n,result);
end;
function strtoint(s:string):int64;
var k:longint;
begin
val(s,result,k);
end;
function IntToBin(n:int64):string;
var t:string; i:longint;
begin
t:='';
while n>0 do begin
      t:=t+inttostr(n mod 2);
      n:=n div 2;
end;
result:='';
for i:=length(t) downto 1 do
    result:=result+t[i];
end;
function BinToInt(B:string):Int64;
var i:byte; k:longint;
begin
result:=0;
k:=length(b);
for i:=1 to k do result:=(result shl 1)+(byte(B[i]) and 1);
end;
begin
assign(input,'input.txt');
assign(output,'output.txt');
reset(input);
rewrite(output);
read(n);
m:=n;
t:=inttobin(n);
k:=length(t)-1;
for i:=1 to k do begin
    t:=t[length(t)]+copy(t,1,k);
    c:=bintoint(t);
    if c>m then m:=c;
end;
write(m);
end.